﻿using Org.BouncyCastle.Crypto.Digests;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace UtilsBase
{
    public class UtilsDateTime
    {
        /// <summary>
        /// 延时
        /// </summary>
        /// <param name="mmseconds"></param>
        public void delay(int mmseconds)
        {
            DateTime current = DateTime.Now;
            while (current.AddMilliseconds(mmseconds) > DateTime.Now)
            {
               // if (current.AddMilliseconds(mmseconds) < DateTime.Now)
               // {
               //     break;
              //  }
              //  Thread.Sleep(0);
            }
        }
        public static string getNowWeekName()
        {
            DayOfWeek thisweek = DateTime.Now.DayOfWeek;
            string WeekName = "";
            switch (thisweek)
            {
                case DayOfWeek.Monday:
                    WeekName = "星期一";
                    break;
                case DayOfWeek.Tuesday:
                    WeekName = "星期二";
                    break;
                case DayOfWeek.Wednesday:
                    WeekName = "星期三";
                    break;
                case DayOfWeek.Thursday:
                    WeekName = "星期四";
                    break;
                case DayOfWeek.Friday:
                    WeekName = "星期五";
                    break;
                case DayOfWeek.Saturday:
                    WeekName = "星期六";
                    break;
                case DayOfWeek.Sunday:
                    WeekName = "星期日";
                    break;
            }
            return WeekName;

        }

        /// <summary>
        /// 时间比较
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public int getTimeCompare(DateTime startTime, DateTime endTime, string type)
        {
            DateTime dt1 = startTime;
            DateTime dt2 = endTime;
            TimeSpan ts = dt2 - dt1;
            if (type == "ss")
            {
                return int.Parse(ts.TotalSeconds.ToString("#0"));
            }
            else if (type == "mm")
            {
                return int.Parse(ts.TotalMinutes.ToString("#0"));
            }
            else if (type == "hh")
            {
                return int.Parse(ts.TotalHours.ToString("#0"));
            }
            else if (type == "dd")
            {
                return int.Parse(ts.TotalDays.ToString("#0"));
            }
            return int.Parse(ts.TotalSeconds.ToString("#0"));
        }

        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <returns></returns>
        public static long getTimeStamp(bool accurateToMilliseconds = true)
        {
            if (accurateToMilliseconds)
            {
                return DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
            }
            else
            {
                return DateTimeOffset.UtcNow.ToUnixTimeSeconds();
            }
        }

        /// <summary>
        /// 时间戳 转换为时间
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static DateTime getStampToDateTime(long timeStamp, bool accurateToMilliseconds = true)
        {
            if (accurateToMilliseconds)
            {
                return DateTimeOffset.FromUnixTimeMilliseconds(timeStamp).LocalDateTime;
            }
            else
            {
                return DateTimeOffset.FromUnixTimeSeconds(timeStamp).LocalDateTime;
            }
            //DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            //long mTime = long.Parse(timeStamp + "0000");
            //TimeSpan toNow = new TimeSpan(mTime);
            //return startTime.Add(toNow);
        }
        public static string getFormatTime(string dateTime)
        {
            if (!string.IsNullOrWhiteSpace(dateTime))
            {
                return Convert.ToDateTime(dateTime).ToString("yyyy-MM-dd HH:mm:ss");
            }
            return dateTime;
        }
        public static string getFormatTime(DateTime dateTime)
        {
            return Convert.ToDateTime(dateTime).ToString("yyyy-MM-dd HH:mm:ss");
        }

        public int getIntervalSeccond(double IntervalTime, string unit)
        {
            //心跳时间
            if (unit == "ss")
            {
                //分钟
                return int.Parse(UtilsNumber.GetNumberForLength(IntervalTime * 1000, 0).ToString());
            }
            else if (unit == "mm")
            {
                //分钟
                return int.Parse(UtilsNumber.GetNumberForLength(IntervalTime * 60 * 1000, 0).ToString());
            }
            else if (unit == "hh")
            {
                //小时
                return int.Parse(UtilsNumber.GetNumberForLength(IntervalTime * 60 * 60 * 1000, 0).ToString());
            }

            return 10000;
        }
    }
}
